package ltd.smallwang.web;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ltd.smallwang.client.task.api.TaskService;
import ltd.smallwang.client.task.dto.TaskAddCmd;
import ltd.smallwang.client.task.dto.TaskCompleteChangeCmd;
import ltd.smallwang.client.task.dto.TaskDto;
import ltd.smallwang.client.task.dto.TaskQry;
import ltd.smallwang.domain.task.Task;
import ltd.smallwang.domain.task.gateway.TaskGateway;
import ltd.smallwang.infrastructure.task.db.TaskMapper;
import ltd.smallwang.web.convert.TaskConvert;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Task controller.
 *
 * @author justd
 */
@RestController
@RequestMapping("/task")
@RequiredArgsConstructor
@Slf4j
public class TaskController {

  private final TaskGateway taskGateway;

  private final TaskMapper taskMapper;

  private final TaskService taskService;

  /**
   * 查询任务列表
   *
   * @param pageIndex the page index
   * @param pageSize  the page size
   * @return the tasks
   */
  @GetMapping(value = "/v1/getTasks", produces = MediaType.APPLICATION_JSON_VALUE)
  public PageResponse<TaskDto> getTasks(@RequestParam("pageIndex") int pageIndex,
      @RequestParam("pageSize") int pageSize) {
    log.info("begin to get tasks");
    Long count = taskMapper.selectCount(null);
    if (count == 0) {
      return PageResponse.of(new ArrayList<>(), 0, pageSize, pageIndex);
    }
    List<Task> taskList = taskGateway.list(null, pageIndex, pageSize);
    List<TaskDto> taskVos = taskList.stream().map(TaskConvert.INSTANCE::convert).toList();
    return PageResponse.of(taskVos, count.intValue(), pageSize, pageIndex);
  }

  /**
   * 获取任务列表，动态条件查询
   *
   * @param taskQry the task qry
   * @return the tasks v 2
   */
  @PostMapping(value = "/v2/getTasks", produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public SingleResponse<List<TaskDto>> getTasksV2(@RequestBody @Validated TaskQry taskQry) {
    return SingleResponse.of(taskService.queryTasks(taskQry));
  }

  /**
   * 创建任务
   *
   * @param taskAddCmd the task add cmd
   * @return the response
   */
  @PostMapping(value = "/v1/createTask", produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public Response createTask(@RequestBody @Validated TaskAddCmd taskAddCmd) {
    log.info("begin to create task");
    taskService.createTask(taskAddCmd);
    return Response.buildSuccess();
  }

  /**
   * 提交任务完成标志变更请求
   *
   * @param taskCompleteChangeCmd 任务完成标志变更命令，包含需要变更的任务信息和新的完成标志。
   *                               必须是一个验证过的对象，确保数据的完整性和正确性。
   * @return 返回一个响应对象，如果变更操作成功，则返回成功的状态和相关数据。
   */
  @PostMapping(value = "/v1/taskCompleteFlagChange", produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.APPLICATION_JSON_VALUE)
  public Response taskCompleteFlagChange(
      @RequestBody @Validated TaskCompleteChangeCmd taskCompleteChangeCmd) {
    log.info("begin to change complete flag");
    taskService.taskCompleteChange(taskCompleteChangeCmd);
    return Response.buildSuccess();
  }
}
